
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class ImagePanel extends JPanel implements MouseMotionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BufferedImage img ;


	public ImagePanel(){

		//		try{
		//			/** lecture de l'image : */
		//			img = ImageIO.read(new File("src/cortus.gif"));
		//		}catch(IOException e){
		//			System.out.println(e);
		//		}
	}

	/**
	 * Change the image which will be drawn on the panel's background
	 * @param image
	 */
	public void setImage(BufferedImage image){
		/** On crée la nouvelle image */
		img = new BufferedImage(
				image.getWidth(),
				image.getHeight(),
				image.getType());

		Graphics g = img.createGraphics();
		g.drawImage(image,0,0,this);
		g.dispose();
	}
	
	public BufferedImage getBufferedImage(){
		return img;
		
	}

	public void draw(){
		this.repaint();

	}

	/**
	 * Overrides JPanel.paintComponent to draw an image on background
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(img != null){
			g.drawImage(img,0,0,this);
		}


	}

	BufferedImage toBufferedImage(Image image) {
		/** On test si l'image n'est pas déja une instance de BufferedImage */
		if( image instanceof BufferedImage ) {
			return( (BufferedImage)image );
		} else {
			/** On s'assure que l'image est complètement chargée */
			image = new ImageIcon(image).getImage();

			/** On crée la nouvelle image */
			BufferedImage bufferedImage = new BufferedImage(
					image.getWidth(null),
					image.getHeight(null),
					BufferedImage.TYPE_INT_RGB );

			Graphics g = bufferedImage.createGraphics();
			g.drawImage(image,0,0,this);
			g.dispose();

			return( bufferedImage );
		} 
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		this.repaint();
		
	}
}
